#!/bin/sh
###############################################################################
##  Author    : 陈贰浩
##  Name      : edw_kylin.sh
##  Functions : 增量更新kylin cube
##  Purpose   : 
##  Revisions or Comments
##  VER        DATE        AUTHOR           DESCRIPTION
##---------  ----------  ---------------  ------------------------------------ 
##  1.0      2019-02-15  陈贰浩           1. CREATED THIS SHELL.
###############################################################################


. ${0%`basename ${0}`}edw_parm.sh
. ${0%`basename ${0}`}edw_func.sh


V_DATA_DATE=$(grep "#V_DATA_DATE#" ${V_PARM_FILE} | awk -F'=' '{print $2}')
V_FREQUENCY=$(grep "#V_FREQUENCY#" ${V_PARM_FILE} | awk -F'=' '{print $2}')
# Define basic parm
V_SCRIPT_NAME=$(basename ${0} | cut -d'.' -f1)
V_OUT_LOGS="${V_SHELL_LOGS}/${V_DATA_DATE}/${V_SCRIPT_NAME}_${V_FREQUENCY}.log"

# Define the output flow
exec 4>&1               # screen output
exec 3>>${V_OUT_LOGS}   # script output
exec 2>&3               # error output
exec 1>&3               # standard output

BEGIN
LOGGER "INFO" "Today batch date is ${V_DATA_DATE}."

if [[ ${V_DATA_DATE:4:4} -eq "1331" ]];then
  LOGGER "INFO" "此账期[${V_DATA_DATE}]为结转数据，不加工."
  END
  exit 0
fi
#月初日期
S_DATE=${V_DATA_DATE}
#换算成秒
START_SECNND=$(date -d ${S_DATE} +%s)
#加上8小时时差并换算成毫秒
START_TIME=$((${START_SECNND}*1000 + ${V_KYLIN_MINUSTIME}))
#月末日期
E_DATE=$(date -d "+1day ${S_DATE}" +"%Y%m%d")
E_SECNND=$(date -d ${E_DATE} +%s)
END_TIME=$((${E_SECNND}*1000 + ${V_KYLIN_MINUSTIME}))

:<<!
 状态分类: ERROR 失败
           FINISHED 成功
           PENDING 等待
           RUNNING 正在运行
           DISCARDED 不再使用
 状态获取方式：
    根据CUBE名称获取CUBE日志，从日志中获取任务ID
    根据任务ID获取任务执行日志，再从日志中获取执行状态
    根据状态判断CUBE是否执行成功
 
           
!
#build前先清除
for CUBE in ${V_KYLIN_CUBE[@]}
do
  LOGGER "INFO" "CUBE[${CUBE}] build开始."
  # 清除历史数据
  LOGGER "INFO" "清除本期数据."
  curl -X DELETE --user ADMIN:KYLIN -H "Content-Type: application/json;charset=UTF-8"  http://${V_KYLIN_LINK}:7070/kylin/api/cubes/${CUBE}/segs/${S_DATE}000000_${E_DATE}000000 
  RE_CODE=$?
  if [[ ${RE_CODE} -ne 0 ]];then
    LOGGER "ERROR" "CUBE[${CUBE}] 数据清除失败！！"
    exit 1
  fi
  #获取CUBE日志
  LOGGER "INFO" "获取CUBE日志."
  LOG=$(curl -X PUT --user ADMIN:KYLIN -H "Content-Type: application/json;charset=utf-8" -d '{ "startTime": '${START_TIME}', "endTime": '${END_TIME}', "buildType": "BUILD"}' http://${V_KYLIN_LINK}:7070/kylin/api/cubes/${CUBE}/rebuild)
  RE_CODE=$?
  if [[ ${RE_CODE} -ne 0 ]];then
    LOGGER "ERROR" "CUBE[${CUBE}] 获取CUBE日志信息失败！！"
    exit 2
  fi
  #获取任务id
  LOGGER "INFO" "获取任务id."
  JOB_ID=$(echo ${LOG} | awk -F',' '{print $1}' | awk -F':' '{print $2}'|sed 's/"//g')
  RE_CODE=$?
  if [[ ${RE_CODE} -ne 0 ]];then
    LOGGER "ERROR" "CUBE[${CUBE}] 获取CUBE任务id失败！！"
    exit 3
  fi
  while true
  do
    #查询任务详情
    LOGGER "INFO" "查询任务详情."
    STA_LOG=$(curl -X GET --user ADMIN:KYLIN -H "Content-Type: application/json;charset=UTF-8"  http://${V_KYLIN_LINK}:7070/kylin/api/jobs/${JOB_ID})
    RE_CODE=$?
    if [[ ${RE_CODE} -ne 0 ]];then
        LOGGER "ERROR" "CUBE[${CUBE}],JOB_ID[${JOB_ID}] 获取任务状态日志信息失败！！"
        exit 4
    fi
    #获取任务状态
    LOGGER "INFO" "获取任务状态."
    STATUS=$(echo ${STA_LOG}| awk -F',' '{print $(NF-1)}')||exit 5 
    STATUS=$(echo ${STATUS} |awk -F':' '{print $2}'|sed 's/"//g')
    RE_CODE=$?
    if [[ ${RE_CODE} -ne 0 ]];then
        LOGGER "ERROR" "CUBE[${CUBE}],JOB_ID[${JOB_ID}] 获取任务状态失败！！"
        exit 5
    fi
    #判断CUBE运行状态
    LOGGER "INFO" "任务状态[${STATUS}]."
    case ${STATUS} in
      ERROR)
          LOGGER "ERROR" "CUBE[${CUBE}] build失败，请手动排查！！"
          exit 6
      ;;
      FINISHED)
          $KYLIN_HOME/bin/kylin.sh org.apache.kylin.storage.hbase.util.DeployCoprocessorCLI $KYLIN_HOME/lib/kylin-coprocessor-*.jar all
          break
      ;;
      ?)
          #sleep 30
      ;;
    esac
    sleep 30
  done
  LOGGER "INFO" "CUBE[${CUBE}] build完成."
done

END
exit 0
